<template>

    <TabGrayBarLineCard :title="'销售额'" @onMore="goDetail(props.permi)" :tabs="card.tabs" :curTab="card.curTab"
        :tabRight="card.tabRight" @onChange="changeTab" :grayCardList="card.grayCardList"
        :echartId="`myEChartsBarLine-${props.permi}-salesVolume`" :echartData="card.echartData" :lineText="card.curTab == '日' ? '上周' : card.curTab == '月' ? '上年' : ''" :barText="card.curTab == '日' ? '本周' : card.curTab == '月' ? '本年' : '销售额'"
        :unitText="``" :updateText="card.updateText"
        v-btnPermi="{ permi: props.permi, noReport: true, isHiden: true, hideList: props.hideList }">
    </TabGrayBarLineCard>

</template>

<script setup>
// import LineEchart from "@/DbComponents/tabGrayBarLineCard";
import TabGrayBarLineCard from '@/DbComponents/tabGrayBarLineCard'
import { toThousands } from '@/utils/number-util'
import dashBoardApi from '@/api/Manage/Business/dashBoard'
import { reactive, watch, computed } from 'vue'
import { reportLog } from '@/utils/reportLog'
import { useRoute, useRouter } from 'vue-router'
const router = useRouter()
const route = useRoute().query
const tabPermi = route.tabPermi

const props = defineProps({
    permi: {
        type: String,
        required: false,
        default: ''
    },
    hideList: {
        type: Array,
        required: false,
    },
    biProjectCode: {
        type: String,
        required: false,
        default: ''
    },
    updateDate: {
        type: String,
        required: false,
        default: ''
    }
})
const card = reactive(
    {
        tabs: [{ label: '按日', value: '日' }, { label: '按月', value: '月' }, { label: '按年', value: '年' }],
        curTab: '日',
        tabRight: '',
        grayCardList: [{ label: '当日', value: '', unit: '万', color: '', des: '' }, { label: '当周日均', value: '', unit: '万', color: '', des: '' }, { label: '环比上周', value: '', unit: '%', color: '', des: '' }],
        unitText: '',
        updateText: '',
        echartData: {},
        // startDate: '',
        // endDate: '',
    }
)
const getCardData = () => {
    dashBoardApi.querySaleCardApi({ biProjectCode: props.biProjectCode, timeType: card.curTab, dataTime: props.updateDate }).then((res) => {
        if (res.data.code == 200) {
            card.tabRight = res.data.data.timeLab
            card.grayCardList[0].value = toThousands(res.data.data.sale)
            card.grayCardList[1].value = toThousands(res.data.data.avgSale)
            card.grayCardList[2].value = toThousands(res.data.data.yoyValue)
            card.unitText = res.data.data.valueUnit
        }
    }).catch(err => {
        console.log(err)
    });
}
const getChartData = () => {
    dashBoardApi.queryNewSaleChartApi({ biProjectCode: props.biProjectCode, timeType: card.curTab, dataTime: props.updateDate }).then((res) => {
        if (res.data.code == 200) {
            card.echartData = res.data.data
        }
    }).catch(err => {
        console.log(err)
    });
}
const goDetail = (permi) => {
    reportLog({
        type: 4,
        resourceKey: permi,
    })
    router.push({
        path: '/page/routeController/index',
        query: {
            rid: 'Manage/Business/salesVolume/wholeScene',
            rTitle: '销售额-整场列表',
            biProjectCode: props.biProjectCode,
            updateDate: props.updateDate,
            tabPermi: tabPermi,
            curTab: card.curTab,
        },
    })
}
const changeTab = (value) => {
    // 切换tab，更换开始和结束日期
    // 按天：开始和结束日期，均为【数据更新日期】
    // 按月：开始日期为【数据更新日期】当月第一天，结束日期为【数据更新日期】
    // 按年：开始日期为【数据更新日期】当年第一天，结束日期为【数据更新日期】
    if (value == '日') {
        card.grayCardList[0].label = '当日'
        card.grayCardList[1].label = '当周日均'
        card.grayCardList[2].label = '环比上周'

        card.grayCardList[0].unit = '万'
        card.grayCardList[1].unit = '万'

        // card.startDate = props.updateDate
    }
    if (value == '月') {
        card.grayCardList[0].label = '当月累计'
        card.grayCardList[1].label = '当月日均'
        card.grayCardList[2].label = '同比上年'

        card.grayCardList[0].unit = '万'
        card.grayCardList[1].unit = '万'

        // card.startDate = dateFormat(new Date(props.updateDate), 'YYYY-MM') + '-01'
    }
    if (value == '年') {
        card.grayCardList[0].label = '当年累计'
        card.grayCardList[1].label = '当年日均'
        card.grayCardList[2].label = '同比上年'

        card.grayCardList[0].unit = '万'
        card.grayCardList[1].unit = '万'

        // card.startDate = dateFormat(new Date(props.updateDate), 'YYYY') + '-01-01'
    }
    // card.endDate = props.updateDate
    card.curTab = value

    getCardData()
    getChartData()
}

changeTab(card.curTab)

const biProjectCode = computed(() => {
    return props.biProjectCode
});
watch(biProjectCode, () => {
    changeTab(card.curTab)
}, { deep: true });

</script>

<style scoped lang="scss"></style>
